<?php

class ping_RPC {

    var $url;
    var $arcurls;
    var $xml;
    public $baidu_tu_token;

    function baidutui() {
        $result = array();
        $site = parse_url($this->arcurls[0], PHP_URL_HOST);
        $domain = get_domain($this->arcurls[0]);
        if (!empty($this->baidu_tu_token)) {
            $api = 'http://data.zz.baidu.com/urls?site=' . $site . '&token=' . $this->baidu_tu_token;
            $ch = curl_init();
            $options = array(
                CURLOPT_URL => $api,
                CURLOPT_POST => false,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => implode("\n", $this->arcurls),
                CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
                CURLOPT_TIMEOUT => 8,
                CURLOPT_CONNECTTIMEOUT => 8,
            );
            curl_setopt_array($ch, $options);
            $content = curl_exec($ch);
            curl_close($ch);
            $content = !empty($content) ? json_decode($content, true) : '';
            if ($content) {
                if (isset($content['error'])) {
                    $result['status'] = -1;
                    $result['errCode'] = 102; // token错误
                } elseif (!empty($content['not_same_site'])) {
                    $result['status'] = -1;
                    $result['errCode'] = 103; //not_same_site
                } elseif (!empty($content['not_valid'])) {
                    $result['status'] = -1;
                    $result['errCode'] = 104; //not_valid
                } else {
                    $result['status'] = 1;
                    $result['remain'] = $content['remain'];
                }
            } else {
                $result['status'] = -1;
                $result['errCode'] = 101; //百度接口异常
            }
        } else {
            $result['status'] = 0;
            $result['errCode'] = 0;
        }
        return $result;
    }

    function ping($yumingid = 0,$limit=100) {
        if (empty($yumingid)) {
            $yuming = db::select("select * from yuming where id={$yumingid} limit 1");
        } else {
            $yuming = db::select("select * from yuming where 1");
        }
        if ($yuming) {
            foreach ($yuming as $ym) {
                $yumingid = $ym['id'];
                $list = db::select("select id from content_{$yumingid} where ping_status=0 order by addtime desc limit $limit");
                if ($list) {
                    $update_ids = $arcurls = array();
                    foreach ($list as $v) {
                        $update_ids[] = $v['id'];
                        $arcurls[] = "http://" . $ym['name'] . "/article/$v[id].html";
                    }
                    $this->arcurls = $arcurls;
                    
                    $errCode = 0;
                    $this->get_baidu_tu_token($yumingid);
                    $result = $this->baidutui();
                    $time = time();
                    $update_id = implode(',', $update_ids);

                    if ($result['status'] == 1) {
                        logging_run("百度推送成功{$yumingid}：".json_encode($result));
                        db::query("update content_{$yumingid} set ping_status = 1 where id in ($update_id)");
                    } else {
                        logging_run("百度推送失败{$yumingid}：".json_encode($result));
                        $errCode = $result['errCode'];
                        db::query("update content_{$yumingid} set ping_status = -1,ping_errMsg='$errCode' where id in ($update_id)");
                    }
                }
            }
        }
    }
    
    
    
    function push_url($yumingid,$urls) {
        $this->arcurls = $urls;
        $this->get_baidu_tu_token($yumingid);
        $result = $this->baidutui();
        if ($result['status'] == 1) {
            $this->redis_push_count($yumingid, $result['success']);
            return true;
        } else {
            return false;
        }
    }
    
    
    //统计推送数据
    function redis_push_count($yumingid,$value) {
        mredis::zinCry('jsc_baidu_push_count_'.date('Ymd'), $yumingid, $value);
    }
    
    
    function get_baidu_tu_token($yumingid) {
        $res=db::find("select baidu_token from seoconfig where yuming_id='$yumingid'");
        if(empty($res)) {
            $res = db::find("select * from `config` where varName='baidu_tui_token'");
            if(!empty($res['varName'])) {
                $this->baidu_tu_token = $res['varName'];
            }
        }else {
            $this->baidu_tu_token = $res['baidu_token'];
        }
    }

}
